package com.wxxymaker.edu.controller.api;

import com.blade.ioc.annotation.Inject;
import com.blade.mvc.annotation.Path;
import com.blade.mvc.annotation.PathParam;
import com.blade.mvc.annotation.PostRoute;
import com.blade.mvc.ui.RestResponse;
import com.wxxymaker.edu.model.entity.Task;
import com.wxxymaker.edu.roles.TeacherRoles;
import com.wxxymaker.edu.service.MaterialService;
import com.wxxymaker.edu.service.TaskService;

/**
 * @AUTHOR soft
 * @DATE 2018/9/9 20:19
 * @DESCRIBE 课程任务设置接口
 */
@TeacherRoles
@Path(value = "/course/set/task", restful = true)
public class TaskSetHandlerApi {

    @Inject
    private TaskService taskService;

    @Inject
    private MaterialService materialService;

    /**
     * 添加任务
     *
     * @param kid  课程id
     * @param task 任务信息对象实例
     */
    @PostRoute("/:kid/add")
    public RestResponse add(@PathParam int kid, Task task) {
        task.setCourseId(kid);
        Task add = taskService.add(task);
        if (add != null) {
            materialService.setTaskId(task.getMaterialId(), add.getId());
            return RestResponse.ok(task);
        }
        return RestResponse.fail("课程添加失败!");
    }

    /**
     * 删除课程任务
     *
     * @param tid 任务id
     */
    @PostRoute("/:kid/delete/:tid")
    public RestResponse delete(@PathParam Integer tid) {
        Task delete = taskService.delete(tid);
        if (delete != null) {
            return RestResponse.ok(delete);
        }
        return RestResponse.fail("删除任务失败!");
    }

    /**
     * 修改课程任务
     *
     * @param tid  任务id
     * @param task 任务实例
     */
    @PostRoute("/:kid/change/:tid")
    public RestResponse change(@PathParam Integer tid,
                               Task task) {
        task.setId(tid);
        if (taskService.change(task)) {
            materialService.setTaskId(task.getMaterialId(), tid);
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    //发布任务
    @PostRoute("/:kid/publish/:tid")
    public RestResponse publish(@PathParam Integer tid) {
        if (taskService.setStatus(tid, 1)) {
            return RestResponse.ok("发布成功!");
        }
        return RestResponse.fail();
    }

    //取消发布任务
    @PostRoute("/:kid/unpublish/:tid")
    public RestResponse unpublish(@PathParam Integer tid) {
        if (taskService.setStatus(tid, 0)) {
            return RestResponse.ok("取消发布成功!");
        }
        return RestResponse.fail();
    }
}